package com.qwt.part_time_api.handler;


import com.qwt.part_time_api.error.ErrorCode;
import com.qwt.part_time_api.utils.JsonAuthentication;
import com.qwt.part_time_api.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.*;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

/**
 * @author 君子慎独
 * 失败处理器
 */
@Component
@Slf4j
public class LocalAuthenticationFailureHandler extends JsonAuthentication implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        log.info("登录失败");
        //e 是UserDetailServiceImpl 的错误传递过来的
        Result fail;

        String isFail = "用户不存在或账号错误";

        if (e instanceof AccountExpiredException) {
            //账号过期
            log.info("账号过期");
            fail = Result.fail(ErrorCode.USER_ACCOUNT_EXPIRED.getErrCode(), ErrorCode.USER_ACCOUNT_EXPIRED.getErrMsg());
        } else if (e instanceof BadCredentialsException) {
            //密码错误
            if (Objects.equals(e.getMessage(), isFail)) {
                log.info("密码错误");
                fail = Result.fail(ErrorCode.USER_CREDENTIALS_ERROR.getErrCode(), ErrorCode.USER_CREDENTIALS_ERROR.getErrMsg());
            } else {
                //账号不存在
                log.info("账号不存在");
                fail = Result.fail(ErrorCode.USER_NOT_EXIST.getErrCode(), ErrorCode.USER_NOT_EXIST.getErrMsg());
            }
        } else if (e instanceof CredentialsExpiredException) {
            //密码过期
            log.info("密码过期");
            fail = Result.fail(ErrorCode.USER_CREDENTIALS_EXPIRED.getErrCode(), ErrorCode.USER_CREDENTIALS_EXPIRED.getErrMsg());
        } else if (e instanceof DisabledException) {
            //账号不可用
            log.info("账号不可用");
            fail = Result.fail(ErrorCode.USER_ACCOUNT_DISABLE.getErrCode(), ErrorCode.USER_ACCOUNT_DISABLE.getErrMsg());
        } else if (e instanceof LockedException) {
            //账号锁定
            log.info("账号锁定");
            fail = Result.fail(ErrorCode.USER_ACCOUNT_LOCKED.getErrCode(), ErrorCode.USER_ACCOUNT_LOCKED.getErrMsg());
        } else if (e instanceof InternalAuthenticationServiceException) {
            if (Objects.equals(e.getMessage(), isFail)) {
                //用户不存在
                log.info("用户不存在");
                fail = Result.fail(ErrorCode.USER_NOT_EXIST.getErrCode(), ErrorCode.USER_NOT_EXIST.getErrMsg());
            } else {
                //账号为空
                log.info("账号为空");
                fail = Result.fail(ErrorCode.USER_ACCOUNT_NOT_NULL.getErrCode(), ErrorCode.USER_ACCOUNT_NOT_NULL.getErrMsg());
            }

        } else {
            //其他错误
            log.info("其他错误:{}", e.getMessage());
            fail = Result.fail(2, "未知错误,联系管理员");
        }
        //继承封装的输出JSON格式类，并调用父类方法即可
        this.writeJson(request, response, fail);
    }
}


